home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 935 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.0 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: Nico Josuttis <nico@bredex.de>
  3. Newsgroups: comp.std.c++
  4. Subject: string::npos is unhandy and dangerous
  5. Date: 2 Apr 1996 15:16:07 GMT
  6. Organization: ?
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <199604020902.LAA06787@bredex.bredex.de>
  9. NNTP-Posting-Host: taumet.eng.sun.com
  10. X-Authentication-Warning: bredex.bredex.de: Host localhost didn't use HELO protocol
  11. X-Mts: smtp
  12. Content-Length: 1196
  13. Originator: clamage@taumet
  14.  
  15.  
  16. Following the standard, all find() member functions for string
  17. return string::size_type and may have the value string::npos,
  18. which is (size_type)-1.
  19.  
  20. I have some problems with that:
  21.  1.) If i understand it right I have to write:
  22.  
  23.     string::size_type pos;
  24.  
  25.     pos = s.find('x');
  26.     if (pos == string::npos)
  27.         // not found
  28.         ...
  29.  
  30.     This seems unhandy und thus dangerous as people might
  31.     try to make the code simpler:
  32.     For example:
  33.     int pos
  34.     pos = s.find('x');
  35.     if (pos == string::npos)
  36.     or even
  37.     if (pos == -1)
  38.  
  39.     The problem is that the test with operator== fails if
  40.     sizeof(size_type) != sizeof(int)
  41.     because unfortunately then
  42.     (size_type)-1 is not -1
  43.     This is due to the conversion to unsigned.
  44.  
  45.     Am I missing something or are these problems known?
  46.     Why wasn't taken a simpler and safer interface?
  47.  
  48.  2.) I wonder what "npos" means.
  49.      Could somebody tell me?
  50.  
  51. --------
  52. Nico                             address: BREDEX GmbH
  53. email:   nico@bredex.de                   Nicolai Josuttis
  54.                                           Fallersleber-Tor-Wall 23
  55. phone:   +49 531 24330-0                  D-38100 Braunschweig
  56. fax:     +49 531 24330-99                 Germany
  57. --------
  58.  
  59.  
  60. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  61. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  62. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  63. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  64. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  65.